package com.ctshk.sapp.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.system.dto.*;
import com.ctshk.rpc.system.req.*;
import com.ctshk.rpc.system.service.ISysAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-11
 */
@Api(value = "国家区域", tags = "国家")
@RestController
@RequestMapping("/sys/area")
public class SysAreaController extends BaseController {

    @DubboReference
    private ISysAreaService areaService;

    @ApiOperation(value = "查询国家区域列表", notes = "查询国家区域列表")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询国家区域列表", operDesc = "查询国家区域列表")
    @PostMapping("/query/list")
    public Result<PageResponse<AreaDTO>> queryList(@RequestBody @Valid AreaQueryReq areaQueryReq) {
        return Result.success(areaService.queryList(areaQueryReq));
    }

    @ApiOperation(value = "條件查询国家区域列表", notes = "條件查询国家区域列表")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @PostMapping("/query/queryListWhere")
    public Result<List<AreaDTO>> queryListWhere(@RequestBody @Valid AreaQueryWhereReq req) {
        return Result.success(areaService.queryListEsWhere(req));
    }

    @ApiOperation(value = "查询国家区域列表tree結構", notes = "查询国家区域列表tree結構")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @PostMapping("/query/queryTree")
    public Result<JSONArray> queryTree(@RequestBody @Valid AreaILevelReq req) {
        return Result.success(areaService.queryListEsByLevelTwo(req));
    }

    @ApiOperation(value = "查询国家区域列表level", notes = "查询国家区域列表level")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @PostMapping("/query/levelList")
    public Result<List<AreaDTO>> queryLevelList(@RequestBody @Valid AreaQueryLevelReq req) {
        return Result.success(areaService.queryListEsByLevel(req));
    }

    @ApiOperation(value = "查询国家列表OneLevel", notes = "查询国家列表OneLevel")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @GetMapping("/query/esOneLevel")
    public Result<List<AreaDTO>> esOneLevel(@RequestParam("name") String name) {
        return Result.success(areaService.esOneLevel(name));
    }

    @ApiOperation(value = "查询城市", notes = "查询城市")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @PostMapping("/queryById/{id}")
    public Result<AreaDTO> queryByEsId(@PathVariable("id") Long id) {
        return Result.success(areaService.queryByEsId(id));
    }

    @ApiOperation(value = "新增国家区域", notes = "新增国家区域")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.ADD, operTitle = "新增国家区域", operDesc = "新增国家区域")
    @PostMapping("/add")
    public Result add(@RequestBody @Valid AreaAddReq areaAddReq) {
        return areaService.add(areaAddReq, getCurrentUser(getRequest()).getId());
    }

    @ApiOperation(value = "修改国家区域", notes = "修改国家区域")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "修改国家区域", operDesc = "修改国家区域")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid AreaAddReq areaAddReq) {
        areaService.update(areaAddReq, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value = "删除国家区域", notes = "删除国家区域")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.DELETE, operTitle = "删除国家区域", operDesc = "删除国家区域")
    @PostMapping("/delete")
    public Result delete(@RequestBody @Valid AreaIdReq areaIdReq) {
        areaService.delete(areaIdReq, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value = "查询国家区域列表", notes = "查询国家区域列表")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询国家区域列表", operDesc = "查询国家区域列表")
    @GetMapping("/query/area/tree")
    public Result<List<AreaTreeDTO>> queryAreaTree() {
        return Result.success(areaService.queryAreaTree());
    }


    @ApiOperation(value = "数据转换", notes = "数据转换")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.ADD, operTitle = "数据转换", operDesc = "数据转换")
    @GetMapping("/test")
    public Result test() {
        areaService.test();
        return Result.success();
    }

    @ApiOperation(value = "查询国际区域列表(模糊查询)", notes = "查询国际区域列表(模糊查询)")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询国际区域列表(模糊查询)", operDesc = "查询国际区域列表(模糊查询)")
    @GetMapping("/query/tree/like/{name}")
    public Result<List<AreaTreeListDTO>> queryAreaLikeName(@PathVariable String name) {
        return Result.success(areaService.queryAreaLikeName(name));
    }

    @ApiOperation(value = "查询国际区域列表(模糊查询Post)", notes = "查询国际区域列表(模糊查询Post)")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询国际区域列表(模糊查询)", operDesc = "查询国际区域列表(模糊查询)")
    @PostMapping("/query/tree/like/name")
    public Result<PageResponse<AreaTreeListDTO>> queryAreaLikeName(@RequestBody @Valid AreaQueryLikeNameReq req) {
        return Result.success(areaService.queryAreaLikeNamePost(req));
    }

    @ApiOperation(value = "根据Id查询国际区域", notes = "根据Id查询国际区域")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "根据Id查询国际区域", operDesc = "根据Id查询国际区域")
    @PostMapping("/query")
    public Result<AreaTreeListDTO> query(@RequestBody @Valid AreaQueryIdReq req) {
        return Result.success(areaService.queryById(req));
    }


    @ApiOperation(value = "查询国家区域列表是否有子集", notes = "查询国家区域列表")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询国家区域列表是否有子集", operDesc = "查询国家区域列表是否有子集")
    @PostMapping("/query/area/hasChildren")
    public Result<AreaHasChildrenDTO> queryAreaHasChildren(@RequestBody @Valid AreaIdReq req) {
        return Result.success(areaService.queryArea(req));
    }

    @ApiOperation(value = "查询国家区域子集", notes = "查询国家区域子集")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询国家区域子集", operDesc = "查询国家区域子集")
    @PostMapping("/query/area/children")
    public Result<AreaChildrenDTO> queryArea(@RequestBody @Valid AreaIdReq req) {
        return Result.success(areaService.queryAreaChildren(req));
    }

    @ApiOperation(value = "查询一級国家区域列表", notes = "查询一級国家区域列表")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询一級国家区域列表", operDesc = "查询一級国家区域列表")
    @GetMapping("/query/first/area")
    public Result<List<AreaHasChildrenDTO>> queryFirstArea() {
        return Result.success(areaService.queryFirstArea());
    }


}
